#!/usr/bin/python 
# -*- coding: utf-8 -*-
#多进程扫描网段
import multiprocessing
from ping3 import ping
 
# ping 网段ip
def ping_host(activeq,notactiveq,ipaddr):
    if ping(ipaddr):
        activeq.put(ipaddr)
    else:
        notactiveq.put(ipaddr)
 
#读取队列数据
def read(q):
    while True:
        if not q.empty():
            value = q.get(True)
            print(value)
        else:
            break
#扫描ip主函数
if __name__ == '__main__':
    #创建进程间通信队列
    manager = multiprocessing.Manager()
    activeq = manager.Queue()
    notactiveq = manager.Queue()
    process_number = 6
    host_list = []
    for ipnum in range(1, 255):
        host_list.append('192.168.17.' + str(ipnum))
     #创建进程池
    pool = multiprocessing.Pool(processes=process_number)
    for ipaddr in host_list:
        pool.apply_async(ping_host,args=[activeq,notactiveq,ipaddr])
    pool.close()
    pool.join()
    #输出正在使用ip
    print("正在使用的IP如下：")
    read(activeq)
    #输出未被使用ip
    print("未被使用的IP如下：")
    read(notactiveq)
